package com.eyaomall.wxmanage.service.impl;

import java.util.Date;

import org.apache.commons.lang.RandomStringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.eyaomall.wxmanage.common.error.BECode;
import com.eyaomall.wxmanage.model.po.DepositUser;
import com.eyaomall.wxmanage.model.po.Invoice;
import com.eyaomall.wxmanage.model.pojo.PayMentClass;
import com.eyaomall.wxmanage.service.DepositUserService;
import com.eyaomall.wxmanage.service.InvoiceService;
import com.eyaomall.wxmanage.service.ShopService;
import com.eyaomall.wxmanage.dao.ShopPaymentRecordDao;
import com.eyaomall.wxmanage.model.vo.ShopPaymentRecordVO;
import com.eyaomall.wxmanage.service.ShopPaymentRecordService;
import com.lycheeframework.core.cmp.kit.PageHTTPObject;
import com.lycheeframework.core.cmp.kit.Pages;
import com.lycheeframework.core.exception.ErrorCodeException;
/**
 * 缴费记录服务
 * @author yuhuafeng
 * @date 2016-9-7
 */
@Service
public class ShopPaymentRecordServiceImpl implements ShopPaymentRecordService {

	@Autowired
	private ShopPaymentRecordDao shopPaymentRecordDao;
	@Autowired
	private ShopService shopService;
	@Autowired
	private InvoiceService invoiceService;
	@Autowired
	private DepositUserService depositUserService;

	
	public Pages<?> page(ShopPaymentRecordVO paymentVO, PageHTTPObject httpObject) {
		return shopPaymentRecordDao.page(paymentVO, httpObject);
	}


	/**
	 * 缴费服务
	 * @param id 缴费单id
	 * @param account 缴费账户
	 * @param type 缴费类型
	 * @param orderNo 缴费支付平台的流水号
	 * @param fee 缴费金额，用于和缴费单比对验证
	 * @return
	 */
	@Transactional(propagation=Propagation.REQUIRED)
	public boolean payment(String id, String type) {
		
		ShopPaymentRecordVO vo=(ShopPaymentRecordVO) shopPaymentRecordDao.query("queryById",id);
		vo.setType(type);
		vo.setStatus("1");
		
		//执行该缴费单的付费项目方法
		PayMentClass pay=new PayMentClass(vo.getKinds());
		if(!CallBack(vo,pay))
			throw new ErrorCodeException(BECode.payment_510005);
		try{
			Invoice invoice = invoiceService.queryByPaymentId(Integer.parseInt(id));
			if(invoice != null){
				invoiceService.changState(invoice.getId());
			}
			DepositUser depositUser = new DepositUser();
			depositUser.setAmount(vo.getAmount());
			depositUser.setBuyyerId(vo.getShopId());
			depositUser.setTradeId(0);
			depositUser.setType("0");
			depositUser.setCategory("1");
			String date = new Date().getTime() + RandomStringUtils.random(4, "1234567890");
			depositUser.setOrderNo(vo.getOrderNo());
			depositUser.setDepositNo(date);
			depositUserService.insert(depositUser);
			
		}catch(Exception e){
			e.printStackTrace();
		}
		
		return shopPaymentRecordDao.update("payment", vo)>0?true:false;
	}

	
	/**
	 * 回调方法，用于付费后回调处理
	 * @return
	 */
	private boolean CallBack(ShopPaymentRecordVO vo,PayMentClass pay){
		boolean flag=false;
		if(pay==null||vo==null)
			return flag;
		switch (pay.getType()) {
		case "1":       //如果是店铺续费
			
			flag=shopService.delayUseTime(vo.getShopId(), Integer.valueOf(pay.getOptional()));
			break;

		default:
			break;
		}
		return flag;
	}



}
